package cn.kgc.educenter.controller;


import cn.kgc.commonutils.JwtUtils;
import cn.kgc.commonutils.R;
import cn.kgc.commonutils.ordervo.UcenterMemberOrder;
import cn.kgc.educenter.entity.UcenterMember;
import cn.kgc.educenter.entity.vo.RegisterVo;
import cn.kgc.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-14
 */
@Api(tags = "用户登录与注册")
@RestController
//@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //会员登录
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member) {
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }
    //注册
    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }
    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id 获取用户信息
        UcenterMember member = memberService.getById(memberId);
        System.out.println("--------"+member);
        return R.ok().data("userInfo",member);

    }
    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        //把member对象里值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return  ucenterMemberOrder;
    }
    //查询莫一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister( @PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister", count);
    }
}

